﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core.Data;
using Nop.Core.Domain.Customers;
using Nop.Core.Caching;
using Nop.Core.Events;
using Nop.Core;

namespace Nop.Services.Customers
{
    public class MerchantApplyService : IMerchantApplyService
    {
        #region Constants
        private const string MERAPP_BY_ID_KEY = "Nop.merchantapply.id-{0}";
        private const string MERAPP_PATTERN_KEY = "Nop.merchantapply.";
        #endregion
 
        #region Fields
 
        private readonly IRepository<MerchantApply> _marAppItemRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
 
        #endregion
 
        #region Ctor

        public MerchantApplyService(IRepository<MerchantApply> marAppItemRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _marAppItemRepository = marAppItemRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
 
        #endregion
 
        #region Methods
 
        #region MerchantApply
         
        /// <summary>
        /// Deletes a news
        /// </summary>
        /// <param name="newsItem">News item</param>
        public virtual void DeleteMerchantApply(MerchantApply Item)
        {
            if (Item == null)
                throw new ArgumentNullException("MerchantApplyItem");

            _marAppItemRepository.Delete(Item);

            _cacheManager.RemoveByPattern(MERAPP_PATTERN_KEY);
 
            //event notification
            _eventPublisher.EntityDeleted(Item);
        }
 
        /// <summary>
        /// Gets a news
        /// </summary>
        /// <param name="newsId">The news identifier</param>
        /// <returns>News</returns>
        public virtual MerchantApply GetMerAppById(int Id)
        {
            if (Id == 0)
                return null;

            string key = string.Format(MERAPP_BY_ID_KEY, Id);
            return _cacheManager.Get(key, () =>
            {
                var n = _marAppItemRepository.GetById(Id);
                return n;
            });
        }
 
        /// <summary>
        /// Gets all news
        /// </summary>
        /// <param name="languageId">Language identifier; 0 if you want to get all records</param>
        /// <param name="dateFrom">Filter by created date; null if you want to get all records</param>
        /// <param name="dateTo">Filter by created date; null if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>News items</returns>
        public virtual IPagedList<MerchantApply> GetAllMerApp(int languageId,
            DateTime? dateFrom, DateTime? dateTo, int pageIndex, int pageSize, bool showHidden = false)
        {
            var query = _marAppItemRepository.Table;

            query = query.OrderByDescending(b => b.applyDate);

            var news = new PagedList<MerchantApply>(query, pageIndex, pageSize);
            return news;
        }

        /// <summary>
        /// Gets all news
        /// </summary>
        /// <param name="languageId">Language identifier; 0 if you want to get all records</param>
        /// <param name="dateFrom">Filter by created date; null if you want to get all records</param>
        /// <param name="dateTo">Filter by created date; null if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>News items</returns>
        public virtual IPagedList<MerchantApply> GetAllMerApp(int pageIndex, int pageSize, bool showHidden = false)
        {
            var query = _marAppItemRepository.Table;

            query = query.OrderByDescending(b => b.applyDate);

            var news = new PagedList<MerchantApply>(query, pageIndex, pageSize);
            return news;
        }

        /// <summary>
        /// Inserts a news item
        /// </summary>
        /// <param name="news">News item</param>
        public virtual void InsertMerApp(MerchantApply item)
        {
            if (item == null)
                throw new ArgumentNullException("MerchantApply");

            _marAppItemRepository.Insert(item);
 
            _cacheManager.RemoveByPattern(MERAPP_PATTERN_KEY);
 
            //event notification
            _eventPublisher.EntityInserted(item);
        }
 
        /// <summary>
        /// Updates the news item
        /// </summary>
        /// <param name="news">News item</param>
        public virtual void UpdateMerApp(MerchantApply Item)
        {
            if (Item == null)
                throw new ArgumentNullException("news");

            _marAppItemRepository.Update(Item);
 
            _cacheManager.RemoveByPattern(MERAPP_PATTERN_KEY);
 
            //event notification
            _eventPublisher.EntityUpdated(Item);
        }


 
        #endregion
        #endregion
    }
}
